import uniPopup from '@/components/uni-popup/uni-popup.vue' // 弹出框组件

export default {
  data() {
    return {
      Password: '', //验证码
      second: 60, //验证码60S
      showText: true, //重新发送验证码
      isDisabled: true, //下一步-灰色
      codeError: false, //验证码错误的时候显示的灰色
      mobile: '', //电话号码
      showmobile: '', //页面显示带**的电话号码
      overtime: false, //过期按钮
      interval: null, //倒计时
      isOnly: false,
	  platform: '', //设备平台信息
    }
  },
  computed: {
    btnActive() {
      if (this.codeError || this.overtime) {
        return false
      }
      return true
    },
  },

  onLoad(e) {
    //电话号码中间4位变成**
    this.mobile = e.UserName
    this.showmobile = e.UserName.replace(/^(\d{3})\d{4}(\d{4})$/, '$1****$2')
    this.sendCode(0) //发送验证码
    // if(!JSON.parse(e.isRegister)){
    // 获取设备信息
    const systemInfo = uni.getSystemInfoSync()
    let plat = ''
    // #ifdef APP-PLUS
    plat = '_APP'
    //  #endif
    // #ifdef H5
    plat = '_H5'
    //  #endif
    // #ifdef MP-WEIXIN
    plat = '_WEIXIN'
    //  #endif
    this.platform = systemInfo.platform
    if(systemInfo.brand){
      this.platform = this.platform + '_' + systemInfo.brand
    }
    if(systemInfo.model){
      this.platform = this.platform  + '_' + systemInfo.model
    }
    this.platform = this.platform + plat
  },
  onShow() {
    this.Password = ''
    this.second = 60
    this.showText = true
    this.codeError = false
    this.overtime = false
  },

  methods: {
    //倒计时
    getCode() {
      this.showText = false
      let times = 60
      this.interval = setInterval(() => {
        if (times > 1) {
          times--
          this.second = times < 10 ? '0' + times : times //小于10秒补 0
        } else {
          clearInterval(this.interval)
          this.second = 60
          this.showText = true
          this.interval = null
        }
      }, 1000)
    },
    //发送验证码
    async sendCode(type) {
      // if (type === 1) { //第一次发送
      //   this.Password = '' //清空验证码
      //   this.isDisabled = true //灰色下一步
      //   this.overtime = false //过期
      // }
      uni.showLoading({
        title: '发送中',
      })
      try {
        const data = await this.$api.user.sendCode({
          mobile: this.mobile,
        })
        this.Password = '' //清空验证码
        this.isDisabled = true //灰色下一步
        this.overtime = false //过期
        uni.hideLoading()
        uni.showToast({
          title: '短信发送成功',
          icon: 'none',
        })
        this.getCode()
      } catch (err) {
        console.log(err)
        uni.hideLoading()
      }
    },
    //实时监测验证码是否输入完毕
    changeInput(e) {
      if (e.length === 6) {
        //验证码输入完毕，并且验证码是正确返回的
        this.isDisabled = false //高亮
        this.Password = e
      } else {
        this.isDisabled = true //灰色
      }
      this.codeError = false
    },

    //保存登录信息
    loginSuccess(param) {
      this.$store.commit('user/setUserInfo', param)
      this.$store.commit('setUserInfo', param)
      //token存储
      uni.setStorageSync('token', param.token)
      uni.setStorageSync('Ztc_B_USER_INFO', param)

      //imei信息存储
      this.$api.user
        .saveImei({
          imei: this.$store.state.user.imei,
          mobile: param.mobile,
        })
        .then(res => {
          console.log(res)
        })

      this.getUserInfo(() => {
        // 测试入口
        // uni.navigateTo({
        //   url: '/pages_login/register/register',
        // })
        // return
        // 身份 公司信息都未认证
        if (param.infoDegree === 'BothNone') {
          this.getUserCompany()
          uni.showToast({
            title: '您的资料尚未认证完成，请继续认证',
            icon: 'none',
          })
        } else if (param.infoDegree === 'IdentityInfoNone') {
          //  身份信息未认证
          uni.showToast({
            title: '您的资料尚未认证完成，请继续认证',
            icon: 'none',
            duration: 3000,
          })
          setTimeout(() => {
            uni.navigateTo({
              url: '/pages_login/authenticationPersonal/authenticationPersonal?phone=' +
								this.mobile,
            })
          }, 3000)
        }
        // else if (param.infoDegree === 'StoreInfoNone') {
        //   //  店铺信息未认证
        //   uni.showToast({
        //     title: '您的资料尚未认证完成，请继续认证',
        //     icon: 'none',
        //   })
        //   setTimeout(() => {
        //     uni.navigateTo({
        //       url: '/pages_login/register/register?phone='+this.mobile,
        //     })
        //   }, 3000)
        // } else if (
        //   param.infoDegree === 'StoreInfoWait' ||
        //   param.infoDegree === 'JoinCompanyWait'
        // ) {
        //   // 审核中
        //   uni.navigateTo({
        //     url: '/pages_login/authenticationState/authenticationState?state=0', //审核状态
        //   })
        // } else if (
        //   param.infoDegree === 'StoreInfoDefault' ||
        //   param.infoDegree === 'JoinCompanyDefault'
        // ) {
        //   // 申请被拒绝
        //   uni.navigateTo({
        //     url: '/pages_login/authenticationState/authenticationState?state=2', //审核状态
        //   })
        // }
        // else if(param.infoDegree==="Success"){
        //       uni.reLaunch({
        //         url: '/pages/index/index',
        //       })
        //     }
        else {
          if (this.isOnly) {
            uni.navigateTo({
              url: '/pages_login/register/register?phone=' + this.mobile,
            })
          } else {
            uni.navigateTo({
              url: '/pages_login/enterCompany/enterCompany?phone=' + this.mobile, //选择公司
            })
          }

        }
      })
			 uni.hideLoading()
    },
    async LoginChooseStore() {
      try {
        var card = this.mobile.replace(/\s/g, '') //正则去掉字符串中所有空格
        let data = {
          'pageIndex': 1,
          'pageSize': 100,
          'mobile': card
        }
        const res = await this.$api.user.LoginChooseStore(data)
        console.log(res)
        if (res.data.list.length === 1) {
          if (res.data.list[0].isOnly) {
            this.isOnly = true
          }
        }
        uni.hideLoading()
      } catch (e) {
        console.log(e)
        uni.hideLoading()
        //TODO handle the exception
      }
    },
    //点击下一步 验证码登录
    async nextStep() {
      uni.showLoading({
        title: '正在登录，请稍等'
      })
      if (this.isDisabled) {
        uni.showToast({
          title: '请输入验证码',
          icon: 'none',
        })
        return
      }
      if (!this.btnActive) {
        uni.showToast({
          title: '验证码过期或错误',
          icon: 'none',
        })
        return
      }
      try {
        this.isDisabled = true //点击提交之后，隐藏高亮按钮
        const res = await this.$api.user.passwordLogin({
          UserName: this.mobile,
          Password: this.Password,
          IsMobile: true,
		  login_Platform: this.platform,
        })
        clearInterval(this.interval) //清除计时器
        this.interval = null
        this.loginSuccess(res.data)
        this.LoginChooseStore()

      } catch (err) {
        console.log('捕获错误', err)
        this.isDisabled = false
        uni.hideLoading()
      }
    },

    getUserCompany() {
      const _this = this
      this.$api.user.getUserCompany().then(res => {
        if (res.data.name) {
          _this.$store.commit('user/setCompanyInfo', {
            name: res.data.name,
            isJoinCompany: !res.data.isNew,
          })
          uni.navigateTo({
            url: '/pages_login/authenticationPersonal/authenticationPersonal?companyName=' +
							res.data.name + '&phone=' + this.mobile,
          })
        } else {
          uni.navigateTo({
            url: '/pages_login/register/register?phone=' + this.mobile,
          })
        }
      })
    },

    //获取用户信息
    async getUserInfo(cb) {
      try {
        const res = await this.$api.user.getUserInfo()
        uni.setStorageSync('userInfoDetail', res.data)
        this.$store.commit('setUserInfoDetail', res.data)
        cb()
      } catch (e) {
        this.isDisabled = false
        //TODO handle the exception
      }
    },
  },
}
